package echat.future.cn.echat.my.view;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;

import java.util.Observable;
import java.util.Observer;

import echat.future.cn.echat.R;
import echat.future.cn.echat.databinding.ActivityManageWithrawBinding;
import echat.future.cn.echat.my.api.UserApi;
import echat.future.cn.echat.my.bean.WithdrawAccount;
import echat.future.cn.echat.my.utils.HttpUtilsRequst;
import echat.future.cn.echat.my.utils.RxUtils;
import echat.future.cn.echat.my.utils.StatusBarUtils;
import echat.future.cn.echat.my.utils.ToastUtils;
import echat.future.cn.echat.my.utils.UserHelper;
import echat.future.cn.echat.my.weight.Navagation.IBaseNavagation;

/**
 * 管理钱包，新增提现账户等
 */
public class ManageWithrawActivity extends BaseActivity<ActivityManageWithrawBinding> implements Observer {


    @Override
    public int layoutId() {
        return R.layout.activity_manage_withraw;
    }


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        StatusBarUtils.hideStatus(this);
    }

    @Override
    public IBaseNavagation navagation() {
        return null;
    }

    @SuppressLint("CheckResult")
    @Override
    public void initView() {
        UserHelper.getWithdrawAccount().addObserver(this);
        RxUtils.clickView(bind.confirm).subscribe(view -> save());
        show();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        UserHelper.getWithdrawAccount().deleteObserver(this);
    }

    private void show(){
        bind.account.setText(UserHelper.getWithdrawAccount().getAlipay_account());
        bind.name.setText(UserHelper.getWithdrawAccount().getAlipay_username());
        bind.bindPohone.setText(UserHelper.getWithdrawAccount().getMobile());
        if(UserHelper.getWithdrawAccount().equals("")){
            bind.delete.setVisibility(View.GONE);
        }else{
            bind.delete.setVisibility(View.VISIBLE);
        }
        bind.delete.setOnClickListener(v -> deletAccount());
    }

    @Override
    public void update(Observable o, Object arg) {
        show();
    }

    private void delte(){

    }

    public void save(){
        if(bind.etAccount.getText().toString().length() == 0){
            toast("请输入正确的支付宝账户");
            return;
        }
        if(bind.etName.getText().toString().length() == 0){
            toast("请输入正确的用户姓名");
            return;
        }
        new HttpUtilsRequst(this,HttpUtilsRequst.retrofitClient.create(UserApi.class)
        .saveWithdrawAccount(UserHelper.getToken(),bind.etAccount.getText().toString(),bind.etName.getText().toString()))
                .setmOnRequstSuccessListener((HttpUtilsRequst.OnRequstSuccessListener<String>) o -> {
                    toast("设置账户成功");
                    loadWithrawAccoount();
                }).start();

    }

    /**
     * 获得提现账户
     */
    private void loadWithrawAccoount(){
        new HttpUtilsRequst<>(this,HttpUtilsRequst.retrofitClient.create(UserApi.class).getWithdrawAccount(UserHelper.getToken()))
                .setmOnRequstSuccessListener((HttpUtilsRequst.OnRequstSuccessListener<WithdrawAccount>) o -> {
                    UserHelper.setAccount(o);
                    UserHelper.getWithdrawAccount().update();
                }).start();
    }


    /**
     * 删除钱包
     */
    private void deletAccount(){
        new HttpUtilsRequst<>(this,HttpUtilsRequst.retrofitClient.create(UserApi.class).deleteWithdrawAccount(UserHelper.getToken()))
                .setmOnRequstSuccessListener((HttpUtilsRequst.OnRequstSuccessListener<String>) o -> {
                    toast("删除钱包");
                    UserHelper.getWithdrawAccount().setMobile("");
                    UserHelper.getWithdrawAccount().setAlipay_username("");
                    UserHelper.getWithdrawAccount().setAlipay_account("");
                    UserHelper.getWithdrawAccount().update();
                });
    }
}
